<?php
namespace Admin\Action;
use Common\Model;
class UserAction extends BaseAction
{
    //protected $list_relation = true;
    
    public function _initialize()
    {
        parent::_initialize();
        
        $this->_name = 'user';
    }
    

    protected function _search()
    {
        $map = array();
      	($keyword = I('keyword','','trim')) && $map['phone'] = array('like', '%' . $keyword . '%');
        $role = I('tbname');
        if(isset($_GET['tbname'])){
        	$map['tbname'] = 1;
        	$this->assign('tbname',true);
        }
        return $map;
    }

    public function _before_index()
    {
        $this->sort = 'id';
        $this->order = 'DESC';
        $big_menu = array(
            'title' => '新增用户',
            'iframe' => U('user/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '200'
        );
        $this->assign('big_menu', $big_menu);
    }
    

    
    public function money()
    {
        if(IS_POST){
            
        }
        
        $this->assign('uid', I('id', '','intval'));
        
        $this->assign('open_validator', true);
        
        if (IS_AJAX) {
            $response = $this->fetch();
            $this->ajaxReturn(1, '', $response);
        } else {
            $this->display();
        }
    }
    
    public function ajax_check_name()
    {
        $name = I('username','','trim');
        $id = I('id','','intval');
        
        $where = array(
            'username'=>$name
        );
        
        if($id){
            $where['id'] = array('neq', $id);
        }
        
        if (M('user')->where($where)->field('id')->find()) {
            $this->ajaxReturn(0, L('adboard_already_exists'));
        } else {
            $this->ajaxReturn();
        }
    }
    
    public function ajax_check_phone()
    {
        $phone = I('phone','','trim');
        $id = I('id','','intval');
        
        $where = array(
            'phone'=>$phone
        );
        
        if($id){
            $where['id'] = array('neq', $id);
        }
        
        if (M('user')->where($where)->field('id')->find()) {
            $this->ajaxReturn(0, L('adboard_already_exists'));
        } else {
            $this->ajaxReturn();
        }
    }
    
	
public function edituser(){
		
if(IS_POST){
$role = I('role','','trim');
$password = I('repassword','','trim');
$status = I('status','','trim');
$where = array(
        'id'=>I('id','','trim')
 );
$data['role']=$role;
$data['webmaster']=I('webmaster','','trim');
$data['webmaster_pid']=intval(I('webmaster_pid','','trim'));
$data['webmaster_rate']=intval(I('webmaster_rate','','trim'));
if($password){
$data['password']=md5($password);
}
$data['last_time']=time();
 $res = M('user')->where($where)->save($data);
 if($res){
return $this->ajaxReturn(1,'修改成功！');
 }else{
 return $this->ajaxReturn(0,'修改失败！');	
	
 }
		
}
		
		
	}
	
	
    public function ajax_check_email()
    {
        $email = I('email','','trim');
        $id = I('id','','intval');
        
        $where = array(
            'email'=>$email
        );
        
        if($id){
            $where['id'] = array('neq', $id);
        }
        
        if (M('user')->where($where)->field('id')->find()) {
            $this->ajaxReturn(0, L('adboard_already_exists'));
        } else {
            $this->ajaxReturn();
        }
    }
    
    public function ajax_upload_img() {
    	
		if($_FILES['img']){
	            $file = $this->_upload($_FILES['img'], 'avatar');
	            if($file['error']) {
	            	$this->ajaxReturn(0,$file['info']);
	            } else {
	             $data['img']=$file['pic_path'];
				 $this->ajaxReturn(1, L('operation_success'), $data['img']);
	            }
	   		 } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
		
    }
}